package cn.tedu.smartsite.user.service.impl;

import cn.tedu.smartsite.base.exception.ServiceException;
import cn.tedu.smartsite.base.response.StatusCode;
import cn.tedu.smartsite.user.mapper.UserMapper;
import cn.tedu.smartsite.user.pojo.dto.UserLoginParam;
import cn.tedu.smartsite.user.pojo.dto.UserQuery;
import cn.tedu.smartsite.user.pojo.vo.UserVO;
import cn.tedu.smartsite.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务: {}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null) {
            //如果主业务遇到问题,需要抛出业务层异常,此时的异常结果由全局异常处理器返回给前端
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public List<UserVO> select(UserQuery userQuery) {
        log.debug("查询用户业务: {}",userQuery);
        List<UserVO> userVO = userMapper.select(userQuery);
        return userVO;
    }

    @Override
    public void delete(Long id) {
        log.debug("删除用户业务: {}",id);
        userMapper.delete(id);
    }

}
